package com.kyivmohyla.kma2.client.presenter.header;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Widget;
import com.kyivmohyla.kma2.client.LoginService;
import com.kyivmohyla.kma2.client.events.LoginEvent;
import com.kyivmohyla.kma2.client.gin.Injector;
import com.kyivmohyla.kma2.client.presenter.Presenter;
import com.kyivmohyla.kma2.client.utils.Dialog;
import com.kyivmohyla.kma2.shared.model.UIStaff;

public class HeaderPresenter implements Presenter {

    private final Display display;
    private final Injector injector = Injector.INSTANCE;
    private UIStaff currentStaff;

    @SuppressWarnings("UnusedDeclaration")
    public HeaderPresenter() {
        this.display = injector.getHeader();
    }

    public HeaderPresenter(UIStaff currentStaff) {
        this.display = injector.getHeader();
        this.currentStaff = currentStaff;
        bind();
        this.display.getFirstName().setText(currentStaff.getFirstName());
        this.display.getLastName().setText(currentStaff.getLastName());
        switch (currentStaff.getPositionId()) {
            case 1:
                this.display.getPosition().setText("(Operator)");
                break;
            case 2:
                this.display.getPosition().setText("(Manager)");
                break;
            case 3:
                this.display.getPosition().setText("(Engineer)");
                break;
        }
    }

    private void bind() {
        this.display.getLogoutButton().addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent clickEvent) {
                HeaderPresenter.this.currentStaff = null;
                LoginService.App.getInstance().logout(new AsyncCallback<Void>() {
                    @Override
                    public void onFailure(Throwable throwable) {
                        Dialog.getInstance().createDialogBoxWithMessage("Error", throwable.getMessage());
                    }

                    @Override
                    public void onSuccess(Void aVoid) {
                    }
                });
                injector.getEventBus().fireEvent(new LoginEvent());
            }
        });
    }

    @SuppressWarnings("UnusedDeclaration")
    public UIStaff getCurrentStaff() {
        return currentStaff;
    }

    @Override
    public void go(HasWidgets container) {
        container.clear();
        container.add(display.asWidget());
    }

    public interface Display {
        HasClickHandlers getLogoutButton();

        HasText getFirstName();

        HasText getLastName();

        HasText getPosition();

        Widget asWidget();
    }
}
